let app = getApp();
// 获取每日必应壁纸
// https://github.com/xCss/bing
// https://bing.ioliu.cn/v1?h=1280&w=768
Page({
  data: {},
  onLoad: function(options) {
    let that = this;
    that.setData({
      ran: parseInt(Math.random() * 4 + 1),
    })
    this.getNeed();
  },
  // 获得必要信息
  getNeed: function() {
    let that = this;
    // 获得坐标
    wx.getLocation({
      success: function(res) {
        app.globalData.location = res;
        that.getWeather();
        // 开始控制跳转
        that.control();
      },
      fail: function(res) {
        console.log('获得位置失败', res)
        wx.reLaunch({
          url: '/pages/bus/bus',
        })
      },
    })
    // 获得OpenId
    wx.login({
      success: function(res) {
        let dataArr = {
          what: "GetOpenid",
          op: res.code,
        }
        app.reGetFn(app.globalData.url, dataArr).then(e => {
          app.globalData.openid = e.data;
        })
      }
    });
  },
  // 控制跳转
  control: function() {
    let that = this,
      interval = setInterval(e => {
        if (app.globalData.openid && app.globalData.location && app.globalData.weather) {
          clearInterval(interval)
          wx.reLaunch({
            url: '/pages/bus/bus',
          })
        }
      }, 300);
  },
  // 获得天气
  getWeather: function() {
    let that = this;
    wx.request({
      url: "https://restapi.amap.com/v3/weather/weatherInfo?city=440400&key=0cef2b0d5709b8f80c48d5ff4b6c5754&extensions=base",
      method: 'get',
      success: function(res) {
        let weather = res.data.lives[0].weather,
          date = new Date();
        let hours = date.getHours(),
          setImg = "/images/weather/";
        if (weather == '晴') {
          setImg = setImg + 'qing';
        } else if (weather == '少云' || weather == '多云' || weather == '阴') {
          setImg = setImg + 'yin';
        } else if (weather == '晴间多云') {
          setImg = setImg + 'duoyun';
        } else if (weather == '有风') {
          setImg = setImg + 'feng';
        } else if (weather == '平静') {
          setImg = setImg + 'taifeng';
        } else if (weather == '小雨') {
          setImg = setImg + 'xiaoyu';
        } else if (weather == '中雨') {
          setImg = setImg + 'zhongyu';
        } else if (weather == '大雨') {
          setImg = setImg + 'dayu';
        } else if (weather == '暴雨') {
          setImg = setImg + 'baoyu';
        } else {
          setImg = setImg + 'taifeng';
        }
        app.globalData.weather = res.data.lives[0];
        app.globalData.weather.weatherImg = setImg + '.png'
      },
    })
  },
})